/**
 * @func 这是一个错误监听事件管理器
 * @msg 挂在函数后触发相关事件监听，实时上报 
 * 
*/
import { EVENTTYPES, STATUS_CODE } from '../../common/constant';//常量
import { handlerEvents } from '../handlerEvents'//错误采集上报控制器
import { addEventHadnler } from './addEventHadnler'
import { htmlElementAsString } from './tools'
import { behaviors } from '../behaviorCollection'
export function setupEvents() {
    // 1. 捕获错误
    addEventHadnler({
        cb: (error) => {
            handlerEvents.collectError(error)
        },
        type: EVENTTYPES.ERROR,
    })

    // 2. 监听click事件---主要是用来进行行为采集的
    addEventHadnler({
        cb: (data) => {
            // 获取html信息
            const htmlString = htmlElementAsString(data.data.activeElement);
            // 行为上报
            if (htmlString) {
                behaviors.push({
                    type: EVENTTYPES.CLICK,
                    status: STATUS_CODE.OK,
                    category: behaviors.getCategory(EVENTTYPES.CLICK),
                    data: htmlString,
                    time: Date.now(),
                });
            }
        },
        type: EVENTTYPES.CLICK
    })
    // 3. 监听hashchange
    addEventHadnler({
        cb: (data) => {
            handlerEvents.collectError(data)
        },
        type: EVENTTYPES.HASHCHANGE,
    })
    // 监听history模式路由的变化
    addEventHadnler({
        cb: (data) => {
            handlerEvents.collectHistory(data)
        },
        type: EVENTTYPES.HISTORY,
    })
    // 5. 重写XMLHttpRequest
    addEventHadnler({
        cb: (data) => {
            handlerEvents.collectHttp(data, EVENTTYPES.XHR);
        },
        type: EVENTTYPES.XHR,
    })
    // 6.重写fetch
    addEventHadnler({
        cb: (data) => {
            handlerEvents.collectHttp(data, EVENTTYPES.FETCH);
        },
        type: EVENTTYPES.FETCH,
    })
    // 添加handleUnhandleRejection事件  promise
    addEventHadnler({
        cb: data => {
            handlerEvents.collectUnhandleRejection(data);
        },
        type: EVENTTYPES.UNHANDLEDREJECTION,
    });
    // 白屏检测



}



